import 'package:edu/networks/common/base_response.dart';
import 'package:edu/networks/common/page_meta.dart';
import 'package:edu/networks/course/models/course_response.dart';
import 'package:intl/intl.dart';
import 'package:json_annotation/json_annotation.dart';

part 'business_response.g.dart';

@JsonSerializable()
class CourseReview {
  // 课程评价id
  @JsonKey(name: 'id')
  int id;
  // 课程id
  @JsonKey(name: 'course_id')
  int courseId;
  // 用户id
  @JsonKey(name: 'user_id')
  int userId;
  // 用户名称
  @JsonKey(name: 'user_name')
  String userName;
  // 评价内容
  @JsonKey(name: 'content')
  String content;
  // 评价分数
  @JsonKey(name: 'rating')
  int rating;
  // 评价时间
  @JsonKey(name: 'review_at')
  DateTime reviewAt;
  // 回复id
  @JsonKey(name: 'reply_id')
  int? replyId;
  // 回复内容
  @JsonKey(name: 'reply')
  String? reply;
  // 回复时间
  @JsonKey(name: 'reply_at')
  DateTime? replyAt;

  CourseReview({
    required this.id,
    required this.courseId,
    required this.userId,
    required this.userName,
    required this.content,
    required this.rating,
    required this.reviewAt,
    this.replyId,
    this.reply,
    this.replyAt,
  });

  factory CourseReview.fromJson(Map<String, dynamic> srcJson) =>
      _$CourseReviewFromJson(srcJson);

  Map<String, dynamic> toJson() => _$CourseReviewToJson(this);
}

// 课程评价列表
@JsonSerializable()
class CourseReviewListResponse extends BaseResponse {
  @JsonKey(name: 'my_review')
  CourseReview? myReview;

  @JsonKey(name: 'reviews')
  List<CourseReview>? reviews;

  @JsonKey(name: 'page_meta')
  PageMeta? pageMeta;

  CourseReviewListResponse({
    required super.code,
    super.message,
    this.myReview,
    this.reviews,
    this.pageMeta,
  });

  factory CourseReviewListResponse.fromJson(Map<String, dynamic> json) =>
      _$CourseReviewListResponseFromJson(json);

  Map<String, dynamic> toJson() => _$CourseReviewListResponseToJson(this);
}

// 我的评价
@JsonSerializable()
class MyReviewResponse extends BaseResponse {
  @JsonKey(name: 'my_review')
  CourseReview? review;

  MyReviewResponse({required super.code, super.message, this.review});

  factory MyReviewResponse.fromJson(Map<String, dynamic> json) =>
      _$MyReviewResponseFromJson(json);

  Map<String, dynamic> toJson() => _$MyReviewResponseToJson(this);
}

// 购买课程
@JsonSerializable()
class PurchaseCourseResponse extends BaseResponse {
  @JsonKey(name: 'total_price')
  double? totalPrice;

  PurchaseCourseResponse({required super.code, super.message, this.totalPrice});

  factory PurchaseCourseResponse.fromJson(Map<String, dynamic> json) =>
      _$PurchaseCourseResponseFromJson(json);

  Map<String, dynamic> toJson() => _$PurchaseCourseResponseToJson(this);
}

// 课程订单
@JsonSerializable()
final class CourseOrder extends CourseBrief {
  @JsonKey(name: 'order_at')
  DateTime orderAt;

  CourseOrder({
    required super.id,
    required super.title,
    required super.image,
    required super.videoCount,
    required super.duration,
    required super.level,
    required super.price,
    required super.author,
    required super.authorId,
    required super.studentCount,
    required this.orderAt,
  });

  factory CourseOrder.fromJson(Map<String, dynamic> json) =>
      _$CourseOrderFromJson(json);

  @override
  Map<String, dynamic> toJson() => _$CourseOrderToJson(this);

  String get orderAtString {
    final format = DateFormat('yyyy-MM-dd HH:mm:ss');
    return format.format(orderAt);
  }
}

// 课程订单列表
@JsonSerializable()
class CourseOrderListResponse extends BaseResponse {
  @JsonKey(name: 'orders')
  List<CourseOrder>? orders;

  CourseOrderListResponse({required super.code, super.message, this.orders});

  factory CourseOrderListResponse.fromJson(Map<String, dynamic> json) =>
      _$CourseOrderListResponseFromJson(json);
  Map<String, dynamic> toJson() => _$CourseOrderListResponseToJson(this);
}

// 购物车中的课程订单列表
@JsonSerializable()
class PurchasingCourseListResponse extends BaseResponse {
  @JsonKey(name: 'orders')
  List<CourseOrder>? orders;
  @JsonKey(name: 'page_meta')
  PageMeta? pageMeta;
  @JsonKey(name: 'total_price')
  double? totalPrice;

  PurchasingCourseListResponse({
    required super.code,
    super.message,
    this.orders,
    this.pageMeta,
    this.totalPrice,
  });

  factory PurchasingCourseListResponse.fromJson(Map<String, dynamic> json) =>
      _$PurchasingCourseListResponseFromJson(json);
  Map<String, dynamic> toJson() => _$PurchasingCourseListResponseToJson(this);
}
